package com.example.myone.Pager;

import static android.content.ContentValues.TAG;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myone.Adapter.RadioArticleAdapter;
import com.example.myone.Base.BasePager;
import com.example.myone.Broadcast.MusicManager;
import com.example.myone.Entity.Article;
import com.example.myone.Entity.ArticleCategoryEnum;
import com.example.myone.Entity.Music;
import com.example.myone.Mapper.ArticleMapper;
import com.example.myone.Mapper.MusicMapper;
import com.example.myone.R;
import com.example.myone.Util.DateDialogUtil;
import com.example.myone.Util.DateTimeUtil;

import java.util.List;
import java.util.Locale;
import java.util.Objects;


@SuppressLint("NewApi")
public class RadioPager extends BasePager implements DateDialogUtil.DateSelectedListener {


    public RadioPager(Context context) {
        super(context);
    }

    private TextView dateText;
    private ImageView datePicker;
    private RecyclerView recyclerView;
    private RadioArticleAdapter adapter;

    @Override
    public void onDateSelected(int year, int month) {
        String selectedDate = String.format(Locale.getDefault(), "%04d-%02d", year, month);
        dateText.setText(selectedDate);
        adapter.updateData(
                ArticleMapper.getArticleByCategoryAndTime(context,
                        ArticleCategoryEnum.RADIO.getMessage(),
                        String.format(Locale.getDefault(), "%04d-%02d", year, month)
                )
        );
    }

    @Override
    public View initView() {
        Log.d(TAG, "==> RadioPager initView");
        View view = LayoutInflater.from(context).inflate(R.layout.radio_pager, null);
        dateText = view.findViewById(R.id.tv_date);
        datePicker = view.findViewById(R.id.btn_date_picker);
        recyclerView = view.findViewById(R.id.recycler_view);
        // 日期选择
        datePicker.setOnClickListener(v -> {
            DateDialogUtil.showYearMonthPicker(context, this);
        });
        return view;
    }


    @Override
    public void initData() {
        super.initData();
        Log.d(TAG, "==> RadioPager initData");
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        List<Article> initialData =
                ArticleMapper.getArticleByCategoryAndTime(context,
                        ArticleCategoryEnum.RADIO.getMessage(),
                        DateTimeUtil.getNowYearMonthStr()
                );
        adapter = new RadioArticleAdapter(context, initialData, this::showMusicDialogAndPlay);
        recyclerView.setAdapter(adapter);
    }

    private void showMusicDialogAndPlay(Article article) {
        Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.radio_pager_music_dialog);
        // 初始化视图及数据
        TextView textViewTitle = dialog.findViewById(R.id.music_title);
        SeekBar seekBar = dialog.findViewById(R.id.music_bar);
        TextView textViewAuthor = dialog.findViewById(R.id.music_author);
        ImageView buttonPlayPause = dialog.findViewById(R.id.music_player);

        textViewTitle.setText(article.title);
        textViewAuthor.setText(article.author);
        // 管理并播放音乐
        Music music = MusicMapper.getMusicByArticleId(context, article.id);
        MusicManager musicManager = MusicManager.getInstance(context);
        musicManager.initMediaPlayer(Objects.requireNonNull(music), article.title, article.author);
        musicManager.setSeekBar(seekBar);
        musicManager.setButtonPlayPause(buttonPlayPause);
        // 绑定点击事件
        buttonPlayPause.setOnClickListener(v -> {
            musicManager.playPauseMusic();
        });
        // 绑定进度条监听器
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    musicManager.getMediaPlayer().seekTo(progress);
                }
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
        // 设置Dialog的位置为页面顶部
        Objects.requireNonNull(dialog.getWindow()).setGravity(Gravity.TOP);
        // 设置Dialog的宽度与页面匹配
        dialog.getWindow().setLayout
                (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();
    }

}
